import requests
from lxml import etree
import re
import  json

"""
拿到zol下 不同类型（风景、卡通 等)下的所有图片入口的url

难点：
进入类型的链接后 里面包含多个图片的下载路径，很麻烦。
解决办法：
直接从script中取到 对应的 oriSize  imgsrc
再替换例中的##size##

"""

# 获取网页
url = 'https://desk.zol.com.cn'
res = requests.get(url)
res.encoding = 'gbk'
# print(res.text)
# xpath 路径
et = etree.HTML(res.text) # <Element html at 0x2b2deac90c0>
short_urls = et.xpath('//div/dl[@class="filter-item first clearfix"]/dd/a/@href')
for i in short_urls[1:]:
    urls = url + i
    # print(urls)
    resp = requests.get(urls)
    res.encoding = 'gbk'
    ets = etree.HTML(resp.text)
    result = ets.xpath('//ul/li/a[@class="pic"]/@href')
    for j in result[2:] :
        pic_url = url + j 
        res_pic = requests.get(pic_url)
        res_pic.encoding = 'gbk'

         #re.S匹配换行符
        obj = re.compile(r'var deskPicArr\s*=(?P<dict>.*?);',re.S)
        content = obj.finditer(res_pic.text)
        for item in content:
            dict_str = item.group('dict')

            # 字符串变成字典
            dict = json.loads(dict_str)
            lists = dict['list'] #  ['xxx','xxxx','xxx']
           
            for list in lists:
                # size = list["oriSize"] #列表的获取de两种方式
                size=list.get('oriSize')
                download_url = list["imgsrc"].replace('##SIZE##',size)

                # / 分隔符 命名
                img_name = download_url.split('/')[-1]
                res_d = requests.get(download_url)
                with open(f'./Test/crawler/img/{img_name}',mode='wb') as  f:
                    f.write(res_d.content)  # 这里是content！！
                

        
 